<?php
/**
 * Created by PhpStorm.
 * User: locove
 * Date: 15-1-26
 * Time: 下午5:52
 */
class Controller_Report_Sufoolish extends Controller_Admin{
    //首页
    public function action_index()
    {
        $this->title='供应商银行卡丢失应急处理';
        $view=View::factory('report/sufoolish');
        $this->response->body($view);
    }


    //查询更新
    public function action_change()
    {
        if(isset($_POST['su_name'])&&$_POST['su_name'])
        {
            //获取供应商ID
            $id_sql="select id from skusystem_suppliers
                    where name='".trim($_POST['su_name'])."'
                    and isavailable=1
                    limit 1";
            require_once(Kohana::find_file('vendor','phprpc/phprpc_client'));
            /*$client = new PHPRPC_Client();
            $client->useService("http://192.168.5.5:802/profitapi/execute");*/
            //$ids = json_decode($client->getinfo($id_sql),true);
            $ids = DB::query(Database::SELECT,$id_sql)->execute('newerp')->as_array();
            if($ids&&isset($ids[0]['id'])){
                $su_id = $ids[0]['id'];

                //获取已经有付款记录的采购单号
                $pay_pu_ids = DB::select('purchaseorder_id')->from('purchaseorder_pays')
                    ->where('supplier_id','=',$su_id)
                    ->execute('newerp')
                    ->as_array(NULL,'purchaseorder_id');

                //获取最新的账号ID
                $ac_id = DB::select('payaccountinfo_id')->from('supplierpayaccount')
                    ->where('supplier_id','=',$su_id)
                    ->where('active_status','=',1)
                    ->limit(1)
                    ->execute('newerp')
                    ->get('payaccountinfo_id',0);

                $result = 0;
                if($ac_id)
                {
                    $re = DB::update('purchasesupplier')
                        ->set(array('payaccountinfo_id'=>$ac_id))
                        ->where('supplier_id','=',$su_id)
                        ->where('createtime','>','2014-12-01 00:00:00');
                    if($pay_pu_ids)
                        $re->where('poid','not in',$pay_pu_ids);

                    $result = $re->execute('newerp');
                }

                if($result)
                    Message::success('更新成功'.$result.'条记录');
                else
                    Message::success('更新成功0条记录');
            }else{
                Message::error('该供应商不存在');
            }
        }
        $this->request->redirect($this->request->referrer());
    }
}